<template>
  <div>
    <el-card>
      <div slot="header">分类管理</div>

      <div class="info" style="font-weight:600;margin-bottom:30px;">乐居分类:</div>
      <el-row :gutter="20">
        <el-col :span="6" v-for="item in uploadPic" :key="item.id">
          <el-card>
            <div slot="header">{{item.name}}</div>
            <div>
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="uploadHeader"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  @click.native="nowId(item.id)"
                  >
                  <img style="width:100%;height:300px;" :src="item.imageUrl" class="avatar">
                </el-upload>
            </div>
          </el-card>
        </el-col>
      </el-row>


      <div class="info" style="font-weight:600;margin:30px 0;">全部分类:</div>

      <el-table
        :data="list"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
          prop="name"
          label="菜单名称"
          width="180">
        </el-table-column>

        <el-table-column
          label="icon"
          align="center"
          width="180">
          <template slot-scope="scope">
            <img style="width:60px;height:60px;" :src="scope.row.icon" alt="">
          </template>
        </el-table-column>

        <el-table-column
          prop="name"
          align="center"
          label="分类名字">
        </el-table-column>

        <el-table-column
          prop="sort"
          align="center"
          label="排序">
        </el-table-column>

        <el-table-column
          align="center"
          label="显示状态">
          <template slot-scope="scope">
            <p>{{scope.row.showStatus==0? '不显示':'显示'}}</p>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="导航栏显示">
          <template slot-scope="scope">
            <p>{{scope.row.navStatus==0? '不显示':'显示'}}</p>
          </template>
        </el-table-column>

        <el-table-column
          prop="description"
          align="center"
          label="描述">
        </el-table-column>

        <el-table-column
          prop="createTime"
          align="center"
          label="创建时间">
        </el-table-column>
      </el-table>


    </el-card>
  </div>
</template>

<script>
import {getAllCategory} from '@/api/product/category';
import mixin from '@/mixins/index';
export default {
  mixins:[mixin],
  data(){
    return{
      //上传的图片
      isId:'',
      uploadPic:[
        {
          imageUrl:'https://bufan-apitown.oss-cn-beijing.aliyuncs.com/bftec/20201118203732237creativity.png',
          id:1,
          name:'创意'
        },
        {
          imageUrl:'https://bufan-apitown.oss-cn-beijing.aliyuncs.com/bftec/20201113162204152fengge.png',
          id:2,
          name:'风格'
        },
        {
          imageUrl:'https://bufan-apitown.oss-cn-beijing.aliyuncs.com/bftec/20201113162219971zhuangshipin.png',
          id:3,
          name:'饰品'
        },
        {
          imageUrl:'https://bufan-apitown.oss-cn-beijing.aliyuncs.com/bftec/20201118165103745dengju.png',
          id:4,
          name:'灯具'
        }
      ],
      //分类数据列表
      list:[],
    }
  },
  created(){
    this.getData()
  },
  methods:{
    // 上传成功后的回调
    handleAvatarSuccess(res, file){
      console.log(res.data.fileUrl)
      this.uploadPic.forEach(val=>{
        if(val.id==this.isId){
          console.log(val)
          val.imageUrl=res.data.fileUrl
        }
      })
      console.log(this.uploadPic)
        
        this.$message.success('上传图片成功')
    },
    // 当前选中的id
    nowId(val){
      this.isId=val
    },
    //查询所有一二级分类
    getData(){
      getAllCategory().then(res=>{
        console.log(res);
        this.list=res.data.items.map(res=>{
          var obj=res.category
          obj.children=res.children
        console.log(this.list);
          return obj
        })
      })
    }
  }
}
</script>

<style lang='scss' scoped>
//上传头像
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>